/*!
 * This file aims to implement the configuration parser to read the
 * application configuration file.
 * @author Mazzer Yannis, Calingarayar Martial
 * @file cfgparser.h
 */
#include <QString>
#include <QStringList>
#include <QMap>
#include <QRegExp>
#include <QDebug>
#include "cfgparser.h"

Config::Config(QString filepath)
{
  QFile cfgfile(filepath);
  if(!cfgfile.open(QIODevice::ReadOnly)) return;

  while(!cfgfile.atEnd())
    {
      str = QString(cfgfile.readLine());
      if(str.isEmpty() || str.contains(QString("#")))
	continue;
      QStringList splitted = str.split("=");
//      int offset = str.indexOf('=');
//      QString key = str.mid(0, offset);
//      QString value = str.mid(offset+1,str.length()-7);
      QString key = splitted[0];
      QString value = splitted[1];
      value.remove(QChar('\n'));
      m_pairs[key] = value ;
    }

}

QString Config::get(const QString &key,
		const QString *defval) const
{
  ConfigMap::const_iterator found = m_pairs.find(key);
  if (found == m_pairs.constEnd())
//    if (defval != NULL)
      return NULL;
//    else
//      {
//	try
//	  {throw 21;}
//	catch(int erreur)
//	  {
//	  }
//
//	//{qDebug << "Unknown key: " << erreur << endl;}
//      }

  else
    return found.value();
}

